import axios from 'axios'
import { ElMessage } from 'element-plus'

// 创建axios实例
const request = axios.create({
  baseURL: '/api',
  timeout: 10000
})
// 请求拦截器
request.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.token = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 20000) {
      return res
    } else {
      ElMessage.error(res.message || '请求失败')
      return Promise.reject(res)
    }
  },
  error => {
    if (error.response?.status === 401) {
      ElMessage.error('请先登录')
      localStorage.removeItem('token')
      window.location.href = '/login'
    } else {
      ElMessage.error(error.message || '网络错误')
    }
    return Promise.reject(error)
  }
)

// 用户相关API
export const userApi = {
  // 用户登录
  login: (data) => request.post('/user/login', data),
  // 用户注册
  register: (data) => request.post('/user/register', data),
  // 获取用户信息
  getUserInfo: (id) => request.get(`/user/${id}`),
  // 更新用户信息
  updateUser: (data) => request.put('/user/update', data)
}

// 活动相关API
export const activityApi = {
  // 获取活动列表
  getActivityList: (params) => request.get('/activity/list', { params }),
  // 获取活动详情
  getActivityDetail: (id) => request.get(`/activity/${id}`),
  // 创建活动
  createActivity: (data) => request.post('/activity/create', data),
  // 更新活动
  updateActivity: (data) => request.put('/activity/update', data),
  // 删除活动
  deleteActivity: (id) => request.delete(`/activity/${id}`),
  // 取消活动
  cancelActivity: (id) => request.put(`/activity/cancel/${id}`),
  // 获取热门活动
  getHotActivities: () => request.get('/activity/hot'),
  // 按价格查询活动
  getActivitiesByPrice: (params) => request.get('/activity/search/price', { params })
}

// 预约相关API
export const bookingApi = {
  // 创建预约
  createBooking: (data) => request.post('/booking/create', data),
  // 获取用户预约列表
  getUserBookings: (userId, params) => request.get(`/booking/user/${userId}`, { params }),
  // 获取预约详情
  getBookingDetail: (id) => request.get(`/booking/${id}`),
  // 取消预约
  cancelBooking: (id) => request.put(`/booking/cancel/${id}`),
  // 确认预约
  confirmBooking: (id) => request.put(`/booking/confirm/${id}`),
  // 支付预约
  payBooking: (id) => request.put(`/booking/pay/${id}`)
}

// 教练相关API
export const coachApi = {
  // 获取教练列表
  getCoachList: (params) => request.get('/coach/list', { params }),
  // 获取教练详情
  getCoachDetail: (id) => request.get(`/coach/${id}`),
  // 创建教练
  createCoach: (data) => request.post('/coach/create', data),
  // 更新教练
  updateCoach: (data) => request.put('/coach/update', data),
  // 删除教练
  deleteCoach: (id) => request.delete(`/coach/${id}`),
  // 切换教练状态
  toggleCoachStatus: (id) => request.put(`/coach/toggle-status/${id}`)
}

// 场地相关API
export const venueApi = {
  // 获取场地列表
  getVenueList: (params) => request.get('/venue/list', { params }),
  // 获取场地详情
  getVenueDetail: (id) => request.get(`/venue/${id}`),
  // 创建场地
  createVenue: (data) => request.post('/venue/create', data),
  // 更新场地
  updateVenue: (data) => request.put('/venue/update', data),
  // 删除场地
  deleteVenue: (id) => request.delete(`/venue/${id}`),
  // 切换场地状态
  toggleVenueStatus: (id) => request.put(`/venue/toggle-status/${id}`)
}

// 课程相关API（实际为活动服务）
export const courseApi = {
  // 获取课程列表（实际为活动列表）
  getCourseList: (params) => activityApi.getActivityList(params),
  // 获取课程详情（实际为活动详情）
  getCourseDetail: (id) => activityApi.getActivityDetail(id)
}

// 默认导出所有API
export default {
  // 用户相关
  login(data) {
    return request.post('/user/login', data);
  },
  register(data) {
    return request.post('/user/register', data);
  },

  // 场馆相关
  getVenues(params) {
    return request.get('/venue/list', { params });
  },
  getVenueById(id) {
    return request.get(`/venue/${id}`);
  },
  createVenue(data) {
    return request.post('/venue/create', data);
  },
  updateVenue(data) {
    return request.put('/venue/update', data);
  },
  deleteVenue(id) {
    return request.delete(`/venue/${id}`);
  },
  toggleVenueStatus(id) {
    return request.put(`/venue/toggle-status/${id}`);
  },

  // 教练相关
  getCoaches(params) {
    return request.get('/coach/list', { params });
  },
  getCoachById(id) {
    return request.get(`/coach/${id}`);
  },
  createCoach(data) {
    return request.post('/coach/create', data);
  },
  updateCoach(data) {
    return request.put('/coach/update', data);
  },
  deleteCoach(id) {
    return request.delete(`/coach/${id}`);
  },
  toggleCoachStatus(id) {
    return request.put(`/coach/toggle-status/${id}`);
  },
};